-- MathPad is a general purpose scientific calculator that uses text input rather than a graphic simulation of a hand held calculator. This allows you to see and edit your entire calculation.
--This file lists the basic capabilities of MathPad mainly by giving short examples. This entire file can be evaluated by MathPad but it is probably more convenient to open a new window, copy a single example at at time and experiment with it.
-- The "examples" folder included with this distribution contains some longer examples and utility functions. These examples show how to implement such things as equation solving, curve fitting, vector calculations and numerical solution of differential equations.
---------- Using MathPad as a calculator ---------------
-- MathPad may be used as a calculator simply by typing in numbers and operators. When the ENTER key is hit, each line is evaluated and the results are inserted into the text.
2*(3+4):14.0
-- MathPad inserts the ":" and the result "14.0". A status line at the bottom of the window will show a "√" to indicate that evaluation is complete. If any problems are encountered, they will be reported in the status line and the cursor will be moved to the problem in the text. You can edit and re-evaluate the text at any time.
-- The RETURN key simply terminates a line. It does not cause any evaluation. The ENTER key is on the numeric keypad (or next to the space bar on powerbook keyboards).
-- An expression will be continued if the line ends with an operator
2 *
(3+4):14.0
-- More than one expression can be placed on a line if they are separated by semicolons.
2*3+4:10.0; 2.1*(.3+4.2):9.4
-- The character # may be used to reference the last result
10*234:2340.0
#+5:2345.0
-- Numbers may be entered in "e" format as in "12.3e-4". The following letter suffixes may also be used: G for e9, M for e6, k for e3, m for e-3, u or µ for e-6, n for e-9, p for e-12.
-- The constant π can be entered via the menu or by typing option-p.
-- Numeric output format can be controlled via the menu.
-- MathPad allows the user to type in equations using variables that will be given numeric values elsewhere in the text.
force=mass*accel
force:160.0
mass=5
accel=32
-- MathPad evaluates the entire text so numeric values can be given before or after they are used. A variable has the same numeric value everywhere in the document.
-- When MathPad cannot calculate a numeric value from the information given, it will print '?' and the name of the variable whose value is unknown.
a=b+c
d=a*4
d:? b?
-- MathPad does not perform algebra. The equation x=y+z (or y+z=x) tells how to compute x given y and z but MathPad does not derive any information about how to compute values for y or z from x.
------------- Functions -----------------------
-- MathPad has most of the usual built-in functions: abs() acos() asin() atan() cos() exp() ln() log() sin() sqrt() tan(). Built-in functions may either be typed in or selected from the menu.
-- The argument units for trig functions can be changed to either degrees or radians with the menu.
-- The built-in function trunc(n) returns the integer part of n.
-- The built-in function sum(expr,var,lower,upper) performs a summation of "expr" while incrementing "var" between "lower" and "upper". The expression given as the first parameter is re-evaluated for each step. Expressions for "lower" and "upper" are evaluated only once.
-- The built-in function rand(n) returns a random number in the range 0 to n. There are 32767 possible values. A rand(0) call re-starts the sequence.
-- The functions count(),det(),read() and write() operate on arrays and are described in a later section.
-- Users can also define their own functions. Functions differ from equations in that their parameter names refer to temporary variables that can take on different values each time the function is called.
Force(mass,accel)=mass*accel
Force(10,12):120.0
Force(6,8):48.0
mass*accel:160.0; -- uses global values from previous example
-- In this example the parameters "accel" and "mass" are separate from the global variables "accel" and "mass". Any variables used in the definition that do not appear in the parameter list will refer to global variables.
-- Functions can be referenced before they are defined.
-- All references to a function must have the same number of parameters.
-- Multiple definitions of function names are not allowed.
-- The value of an expression can be made dependent on a condition.
p = -a when a < 0,
1 when a = 0,
a^2 otherwise
-- The result will be the value corresponding to first condition in the list that evaluates true. Commas separate expressions in the list.
-- The keyword "otherwise" is optional and is used only to help readability.
-- The conditional follows the keyword "when" and may use the following operators: = != > < >= <= "and" "or" "not" "known" "unknown". The option key versions of comparison operators are also accepted (≠, ≥, ≤). Note that in this context "=" is a comparison operator. The C style operator "==" is also accepted.
-- The prefix operators "known" and "unknown" can be used to test if a numeric value can be calculated for the given expression.
-- Parenthesis are used only for arithmetic expressions and can not be used to group conditional operators. Arbitrarily complex conditionals can still be built by using multiple clauses and nested conditionals.
-- Comma separated expressions can be used with or without conditionals. The result will be the first item in the list that evaluates to a numeric value. Note that this means:
zz when 1==1,42:42.0; -- result is 42 because zz is unknown
-- MathPad allows recursive function definitions. Depth of recursion is limited by available memory space. If memory runs low during evaluation, recursion is stopped.
-- Evaluation can also be stopped by typing Command-period.
fact(n) = 1 when n < 2, fact(n-1)*n when n > 0
fact(50):3.0e+64
-------------------- Plots ----------------------
-- MathPad can produce simple plots by using the "plot" command.
Xmin=0; Xmax=20
Ymin=0; Ymax=Xmax^2
plot X^2
-- Plotting is controlled by special variables. The independent variable "X" is stepped between "Xmin" and "Xmax" with a resolution of "Xsteps" (default Xsteps=100). The expression values are plotted between "Ymin" and "Ymax". The variable names 'X' (upper case), Xmin, Xmax, Xsteps, Ymin and Ymax should not be used for anything other than plotting.
-- Evaluation can be stopped by typing Command-period.
-- A status line at the bottom of the plot window shows the lowest and highest Y values ("Ylo" and "Yhi") calculated during the plot.
-- If Ymin and/or Ymax are not specified MathPad will auto-range based on Ylo and Yhi.
-- Multiple traces can be plotted simply by adding plot statements. All traces use the same values of "Xmin", "Xmax" , "Xsteps", "Ymin" and "Ymax". The "Ylo" and "Yhi" values apply over all traces.
-- Clicking the mouse on the plot will show the trace number and the X,Y value of the plotted point nearest to the click.
-- The plot window is normally left behind the text window since all editing takes place in the text window. It works best to size the text window and plot window so that they are both visible.
-- A plot can be copied to the clipboard as a PICT. The "Copy Plot" menu items are enabled when the plot window is in front. The clipboard image will be the same size as the current plot window size. The "Copy Plot at 4X" command scales the image up to provide a higher resolution image for applications that can import and re-size PICTs.
-- A plot can be printed. For printing the plot is re-sized to the page regardless of the current plot window size. Printing is done at the highest printer resolution so it may be useful to increase Xsteps for a smoother plot.
-- "label" statements can be used to put a lines from the text window onto the plot. The text following "label" is evaluated normally. This allows labels that will automatically update when the text is re-evaluated.
label Ymax:400.0
-- Arbitrary text labels may be pasted into the plot window. Selecting "Paste Label" from the Edit menu will paste text from clipboard into the upper left corner of the plot. Command-V will paste the label at the current mouse position if it is in the plot window.
-- Labels can be moved by dragging with the mouse.
-- Labels can be deleted by dragging them off the plot.
-- MathPad can produce tables of numeric results using the "table" command.
-- Multiple column tables can be produced by giving a list of expressions separated by commas.
Nmin=1; Nmax=5; Nsteps=5
table N, N^2, N^3
1.0 1.0 1.0
2.0 4.0 8.0
3.0 9.0 27.0
4.0 16.0 64.0
5.0 25.0 125.0
-- Tables are handled in the same way as plots except that the results are inserted as columns in the text window. For tables the independent variable is "N" and the control variables are "Nmin", "Nmax" and "Nsteps".
-- Nsteps is limited to 1000 to prevent accidents from inserting huge amounts of text. Use the write() function for large tables.
-- Do not add any text to the output columns because the table command will not be able to properly delete the table before re-evaluating.
-- If table is used with a single expression that is a 1D or 2D array, the array elements will be printed out. The values of Nmin,Nmax and Nsteps are ignored.
------------------- Arrays -------------
A={10,20,30} -- Defines a 3 element array.
A[2]:20.0; -- Accesses the 2nd element. Index values start at 1.
B[1,2]:20.0; -- Both forms of indexing are allowed
A[2:3]:{20.0,30.0}; -- [lo:hi] selects a sub array.
A[2:3][2:2]:{30.0}; -- consecutive selections operate on the same dimension
A[:2]:{10.0,20.0}; -- lo and/or hi may be omitted.
B[2:]:{{40.0,50.0,60.0},{11.0,21.0,31.0}}
Q[i]=i*11 --Arrays can be defined in terms of their index values.
Q[2]:22.0
--dim[] is used to set the number of elements for an array definition
I[i,j] = 1 when i=j,0 otherwise dim[3,3]
I:{{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}
--Arrays may be used freely in expressions. Operations are performed on each element. Scalars are extended to contain as many elements as needed.
A+Q:{21.0,42.0,63.0}
2+A:{12.0,22.0,32.0}
log(A):{1.0,1.3,1.5}
A*Q:{110.0,440.0,990.0}; --Note: this is not matrix multiply
Q[A]:? A[?]; --Arrays may not be used as index values.
--Functions can use index values
skip(zz,by)[i] = zz[i*by]
skip(Q,2):{22.0,44.0,66.0,...}
multiply(A,B)[i,j] = sum(A[i,k]*B[k,j],k,1,count(B)) dim
[count(A),count(B[1])]
multiply({A},I):{{10.0,20.0,30.0}}
--------- Built-in functions for arrays
count(A):3.0; -- count() returns the number of array elements
count(B):3.0; -- elements in 1st dimension
count(42):0.0; -- scalar
count(Q):?; -- infinite array
det(B):580.0; -- calculates the determinant of a square matrix
--------- Data Files
-- read("filename") returns an array of values read from the named data file. Double quotes are needed if the filename contains blanks or punctuation. The data file is assumed to be in the same folder as the source document. Mac style pathnames such as "Hard Disk:DataFolder:file47" are allowed.
-- write("filename",array) writes the elements of the 1D or 2D array to the named file.
-- See "Datafiles.doc" for more information.
--------- Plotting Arrays
-- The plot command can also plot points from 1D or 2D arrays.
-- For 1D {y1,y2,y3...} the element values are used for y. X is stepped from Xmin to Xmax. To connect the points use "plotline" instead of "plot".
-- 2D arrays can be given in 3 forms:
-- array of x,y pairs {{x1,y1},{x2,y2},{x3,y3},...}
-- a pair of arrays {{x1,x2,...},{y1,y2,...}}
-- a pair of functions {fx(X),fy(X)}
-- For 2D arrays both x and y are taken from the array. X is stepped from 0 to 1.0 and can be used for parametric equations:
deg=X*360; Xsteps=36 -- run deg from 0 to 360 in 10° steps
x(angle)=5*cos(angle)+10 -- arbitrary scale to fit on existing plot
y(angle)=100*sin(angle)+200
plot {x(deg),y(deg)}
-- When plotting x,y points, if Xmin and/or Xmax are not specified they will be auto-ranged. When plotting a mix of arrays and functions, the array should be plotted first to take advantage of X auto-ranging.
--------- Image display
-- The "image" command can be used to display a 2D array. The entire array is displayed and fills the plot area regardless of the values of Xmin, Xmax, Ymin, or Ymax. Scale is controlled by special variables "Zmin" and "Zmax". If Zmin and/or Zmax are not specified MathPad will use Zlo and/or Zhi from the previous evaluation. Display type is controlled by the Display... menu item. For more information see the file "Image.doc"
--(It is better to evaluate this example separately. The example plot is already overcrowded.)
~
arr[i,j] = i+j dim[10,10]
Zmin=2; Zmax=20
image arr
~
------------ Assignments ----------------------
-- The assignment operator := evaluates the right hand side expression and replaces any previous value of the left hand variable with the result. Accessing an assigned variable gives its current value and does not re-evaluate the expression. Assigned variables do not necessarily have the same value everywhere in the text. This differs from = which gives a single definition of how to calculate the value of a variable.
aa:=3:
aa:3.0
aa:=aa+2:
aa:5.0
-- Assignments can only be done to global variables.
-- Entire arrays may be assigned (if they are finite). Individual array elements can not be assigned.
-- The result of the := operation itself is always unknown. If you wish to access the value you must use the variable name. This is done so that multiple assignments can be performed using expression lists.
-- If you want MathPad to act like a desk accessory, turn on the "Save and Quit" option via the Options... menu and save a document in the Apple Menu Items folder. For documents saved with this option on, clicking the window's close box will save the current text and quit the application. This is similar to the way desk accessory calculators operate. The option affects only the action of the close box. If you choose Close from the File menu, the document will be closed but the application will stay active.
-- If you want MathPad's close box to act like most applications, leave this option off.
-- File format
-- MathPad documents are generic TEXT files. Option and plot label information is stored in the resource fork. TEXT files created by other applications may be opened by MathPad and vice versa. The resource fork is not modified until the file is saved. Documents are limited to a maximum size of 30K bytes. Data files can be larger than 30K.
-- Memory size
-- Use the Finder's Get Info... box to set MathPad's memory size depending on your use:
-- 150K if you never use large files, image, read() or write().
-- 160K if you use advanced features and large files.
-- 200K if you use advanced features, large files and large arrays.
-- >200K if you run out of memory when set at 200K. Huge arrays?
-- Data files are read into temporary memory. Large data files can be read if there is enough system memory available. Increasing MathPad's size will not allow larger data files but quitting other applications will.
-- Recursive functions can require large amounts of memory but increasing the MathPad's size past a certain point will not allow deeper recursion. This is because evaluation of recursive functions also uses up stack space which has a fixed allocation size.
-- Background computation
-- Clicking in another application's window will switch even when MathPad is busy with a computation. MathPad will continue computation in the background. The plot window is hidden during background computation and is redrawn when the computation is complete.
-- Background switching is disabled under Multifinder if any read() operations are done. This restriction does not apply under system 7.
--------- Distribution -------------------
-- MathPad is copyright 1993 by Mark Widholm. It is free for non-commercial distribution. Selling it for profit without my permission is forbidden.
-- If you use MathPad, send me some e-mail. I can notify you of any bugs, updates etc.
-- Send comments, questions, suggestions and bug reports to:
-- Widholm@UNH.edu
-- Let me know if you are interested in seeing more examples or if you have a MathPad example that others might be interested in seeing.